package com.jy.wms.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.joyi.gate.safety.vo.User;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.annotions.BaseServiceDescribe;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.common.base.TurnoverRules;
import com.jy.wms.dao.WmsShelvesMapper;
import com.jy.wms.pojo.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: WmsShelvesService.java
 * @Description: 上架
 * @Author yjs
 * @Date Tue Sep 26 18:23:43 CST 2017
 */

@Service
public class WmsShelvesService extends BaseService<WmsShelvesMapper,WmsShelves> {
    @Autowired
    private WmsPalletSortingService wmsPalletSortingService;
    protected static Logger log = Logger.getLogger(WmsShelvesService.class);
    /**
     * 上架管理分页查询
     * @param entity
     * @param ckmc
     * @return
     * @throws ServiceException
     */
   @BaseServiceDescribe(moduleName = "上架管理", btnName = "分页查询上架信息")
    public List<WmsShelves> baseSelectPage(WmsShelves entity, String ckmc) throws ServiceException {
        try {
            List<WmsShelves> wmsShelvesList = new ArrayList<WmsShelves>();
            List<WmsShelves> list = new ArrayList<WmsShelves>();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Calendar cs= Calendar.getInstance();
                if(entity.getCjsj_start()!=null && !"".equals(entity.getCjsj_start())){
                    cs.setTime(new Date(Long.valueOf(entity.getCjsj_start())));
                    entity.setCjsj_start(sdf.format(cs.getTime()));

                    cs.setTime(new Date(Long.valueOf(entity.getCjsj_end())));
                    entity.setCjsj_end(sdf.format(cs.getTime()));
                    //查询全部预期到货通知单集合

                }
                if(entity.getShsj_start()!=null && !"".equals(entity.getShsj_start())){
                    cs.setTime(new Date(Long.valueOf(entity.getShsj_start())));
                    entity.setShsj_start(sdf.format(cs.getTime()));

                    cs.setTime(new Date(Long.valueOf(entity.getShsj_end())));
                    entity.setShsj_end(sdf.format(cs.getTime()));
                }
            list = dao.baseSelectPage(entity);
            //货主订单类型表:是否质检(0否 1是)
            String sfzj1 = "1";
            //0初始 1质检单创建 2部分质检 3全部质检 4 质检完成
            String zjzt2 = "4";
            //预期到货单明细表:质检状态(0初始 1质检单创建 2质检合格 3质检不合格)
            String zjzt = "2";
            //预期到货单明细表:质检状态(0初始 1质检单创建 2质检合格 3质检不合格)
            String zjzt3 = "3";
            //货主表:质检方式(0不质检 1收货后质检 2上架后质检)
            String zjfs1 = "1";
            //查询当前货主信息
            WmsOwner wmsOwner = dao.searchZjfs(entity.getHzid());
            //人工指定库位上架(取asn行表,收货表数据)
            String sjzzgz1 = TurnoverRules.gzdm1.getType();
            //系统按库位组指定上架(取上架表数据)==>库位高度不同,空库位优先
            String sjzzgz2 = TurnoverRules.gzdm2.getType();
            for (WmsShelves wmsShelves : list) {
                wmsShelves.setCkmc(ckmc);
                wmsShelves.setHzid(entity.getHzid());
                wmsShelves.setCkid(entity.getCkid());
                String sfzj2 = dao.searchDdlx(wmsShelves.getYqdhbdid(), wmsShelves.getHzid());
                //判断货主的质检方式
                if (sjzzgz1.equals(wmsOwner.getSjgzid())) {
                    //1收货后质检;1是质检
                    if (zjfs1.equals(wmsOwner.getZjfs())) {
                        //判断预期到货通知单是否质检
                        if (sfzj1.equals(sfzj2)) {
                            WmsEstimatedArrivalOrder wmsEstimatedArrivalOrder = dao.searchYqdh(wmsShelves.getYqdhbdid(), zjzt2);
                            if (wmsEstimatedArrivalOrder != null) {
                                List<WmsShelves> list2 = dao.baseSelectDetail(wmsShelves.getYqdhbdid(), wmsShelves.getCkid(), wmsShelves.getHzid(), zjzt, zjzt3);
                                //收货表有符合上架的数据
                                if (list2.size() > 0) {
                                    wmsShelvesList.add(wmsShelves);
                                }
                            }
                        } else {
                            List<WmsShelves> list2 = dao.baseSelectDetail(wmsShelves.getYqdhbdid(), wmsShelves.getCkid(), wmsShelves.getHzid(), "", zjzt3);
                            //收货表有符合上架的数据
                            if (list2.size() > 0) {
                                wmsShelvesList.add(wmsShelves);
                            }
                        }
                    } else {
                        List<WmsShelves> list2 = dao.baseSelectDetail(wmsShelves.getYqdhbdid(), wmsShelves.getCkid(), wmsShelves.getHzid(), "", zjzt3);
                        //收货表有符合上架的数据
                        if (list2.size() > 0) {
                            wmsShelvesList.add(wmsShelves);
                        }
                    }
                } else if (sjzzgz2.equals(wmsOwner.getSjgzid())) {
                    //1收货后质检;1是质检
                    if (zjfs1.equals(wmsOwner.getZjfs())) {
                        if (sfzj1.equals(sfzj2)) {
                            WmsEstimatedArrivalOrder wmsEstimatedArrivalOrder = dao.searchYqdh(wmsShelves.getYqdhbdid(), zjzt2);
                            if (wmsEstimatedArrivalOrder != null) {
                                List<WmsShelves> list2 = dao.baseSelectDetail2(wmsShelves.getYqdhbdid(), wmsShelves.getCkid(), wmsShelves.getHzid(), zjzt, zjzt3);
                                //收货表有符合上架的数据
                                if (list2.size() > 0) {
                                    wmsShelvesList.add(wmsShelves);
                                }
                            }
                        } else {
                            List<WmsShelves> list2 = dao.baseSelectDetail2(wmsShelves.getYqdhbdid(), wmsShelves.getCkid(), wmsShelves.getHzid(), "", zjzt3);
                            //收货表有符合上架的数据
                            if (list2.size() > 0) {
                                wmsShelvesList.add(wmsShelves);
                            }
                        }
                    } else {
                        List<WmsShelves> list2 = dao.baseSelectDetail(wmsShelves.getYqdhbdid(), wmsShelves.getCkid(), wmsShelves.getHzid(), "", zjzt3);
                        //收货表有符合上架的数据
                        if (list2.size() > 0) {
                            wmsShelvesList.add(wmsShelves);
                        }
                    }
                }
            }
            return wmsShelvesList;
        } catch (Exception e) {
            throw new ServiceException("查询上架信息失败", e);
        }
    }
    /**
     * 上架管理分页查询
     * @param entity
     * @param ckmc
     * @return
     * @throws ServiceException
     */
    @BaseServiceDescribe(moduleName = "上架管理", btnName = "分页查询上架信息")
    public Page<WmsShelves> baseSelectPages(WmsShelves entity, String ckmc) throws ServiceException {
        try {
            Page<WmsShelves> list = new Page<>();
           /* //查询全部预期到货通知单集合
            List<WmsShelves> list = dao.baseSelectPage(entity);*/
            //货主订单类型表:是否质检(0否 1是)
            String sfzj1 = "1";
            //0初始 1质检单创建 2部分质检 3全部质检 4 质检完成
            String zjztd = "4";
            //预期到货单明细表:质检状态(0初始 1质检单创建 2质检合格 3质检不合格)
            String zjzthg = "2";
            String zjztbhg = "3";
            //货主表:质检方式(0不质检 1收货后质检 2上架后质检)
            String zjfs = "1";
            //查询当前货主信息
            WmsOwner wmsOwner = dao.searchZjfs(entity.getHzid());
            //人工指定库位上架(取asn行表,收货表数据)
            String sjzzgzr = TurnoverRules.gzdm1.getType();
            //系统按库位组指定上架(取上架表数据)==>库位高度不同,空库位优先
            String sjzzgzx = TurnoverRules.gzdm2.getType();
            /*for (WmsShelves wmsShelves : list) {*/
            entity.setCkmc(ckmc);
            entity.setHzid(entity.getHzid());
            entity.setCkid(entity.getCkid());
            String sfzj2 = dao.searchDdlx(entity.getYqdhbdid(), entity.getHzid());
            //判断货主的质检方式
            if (sjzzgzr.equals(wmsOwner.getSjgzid())) {
                //1收货后质检;1是质检
                if (zjfs.equals(wmsOwner.getZjfs())) {
                    //判断预期到货通知单是否质检
                        if (sfzj1.equals(sfzj2)) {
                            PageHelper.startPage(entity.getPage(),entity.getRows());
                            list = (Page<WmsShelves>)dao.baseSelectPages(entity,"4","2","3");
                            /*WmsEstimatedArrivalOrder wmsEstimatedArrivalOrder = dao.searchYqdh(wmsShelves.getYqdhbdid(), zjzt2);
                            if (wmsEstimatedArrivalOrder != null) {
                                List<WmsShelves> list2 = dao.baseSelectDetail(wmsShelves.getYqdhbdid(), wmsShelves.getCkid(), wmsShelves.getHzid(), zjzt,zjzt3);
                                //收货表有符合上架的数据
                                if (list2.size() > 0) {
                                    wmsShelvesList.add(wmsShelves);
                                }
                            }*/
                       } else {
                            PageHelper.startPage(entity.getPage(),entity.getRows());
                            list = (Page<WmsShelves>)dao.baseSelectPages(entity,null,null,"3");
                            /*List<WmsShelves> list2 = dao.baseSelectDetail(wmsShelves.getYqdhbdid(), wmsShelves.getCkid(), wmsShelves.getHzid(), "",zjzt3);
                            //收货表有符合上架的数据
                            if (list2.size() > 0) {
                                wmsShelvesList.add(wmsShelves);
                            }*/
                        }
                } else {
                    PageHelper.startPage(entity.getPage(),entity.getRows());
                    list = (Page<WmsShelves>)dao.baseSelectPages(entity,null,null,"3");
                        /*List<WmsShelves> list2 = dao.baseSelectDetail(wmsShelves.getYqdhbdid(), wmsShelves.getCkid(), wmsShelves.getHzid(), "",zjzt3);
                        //收货表有符合上架的数据
                        if (list2.size() > 0) {
                            wmsShelvesList.add(wmsShelves);
                        }*/
                }
            } else if (sjzzgzx.equals(wmsOwner.getSjgzid())) {
                //1收货后质检;1是质检
                if (zjfs.equals(wmsOwner.getZjfs())) {

                        if (sfzj1.equals(sfzj2)) {
                            PageHelper.startPage(entity.getPage(),entity.getRows());
                            list = (Page<WmsShelves>)dao.baseSelectPages(entity,"4","2","3");
                            /*WmsEstimatedArrivalOrder wmsEstimatedArrivalOrder = dao.searchYqdh(wmsShelves.getYqdhbdid(), zjzt2);
                            if (wmsEstimatedArrivalOrder != null) {
                                List<WmsShelves> list2 = dao.baseSelectDetail2(wmsShelves.getYqdhbdid(), wmsShelves.getCkid(), wmsShelves.getHzid(), zjzt,zjzt3);
                                //收货表有符合上架的数据
                                if (list2.size() > 0) {
                                    wmsShelvesList.add(wmsShelves);
                                }
                            }*/
                       } else {
                            PageHelper.startPage(entity.getPage(),entity.getRows());
                            list = (Page<WmsShelves>)dao.baseSelectPages(entity,null,null,"3");
                            /*List<WmsShelves> list2 = dao.baseSelectDetail2(wmsShelves.getYqdhbdid(), wmsShelves.getCkid(), wmsShelves.getHzid(), "",zjzt3);
                            //收货表有符合上架的数据
                            if (list2.size() > 0) {
                                wmsShelvesList.add(wmsShelves);
                            }*/
                        }
                } else {
                    PageHelper.startPage(entity.getPage(),entity.getRows());
                    list = (Page<WmsShelves>)dao.baseSelectPages(entity,null,null,"3");
                        /*List<WmsShelves> list2 = dao.baseSelectDetail(wmsShelves.getYqdhbdid(), wmsShelves.getCkid(), wmsShelves.getHzid(), "",zjzt3);
                        //收货表有符合上架的数据
                        if (list2.size() > 0) {
                            wmsShelvesList.add(wmsShelves);
                        }*/
                }
            }
            /*}*/
            /*if (wmsShelvesList.size() > 0) {
                //开始的数量
                Integer start = entity.getRows() * (entity.getPage() - 1);
                //结束的数量
                Integer end = entity.getRows() * entity.getPage();
                if (end > wmsShelvesList.size()) {
                    end = wmsShelvesList.size();
                }
                wmsShelvesList = wmsShelvesList.subList(start, end);
            }*/
            return list;
        } catch (Exception e) {
            throw new ServiceException("查询上架信息失败", e);
        }
    }
    /**
     * ASN编号模糊查询
     * @param asnbh ASN编号模糊查询
     * @return Page<WmsShelves>
     */
    public List<WmsShelves> searchAsnbh(String asnbh, String ckid) throws ServiceException {
        try {
            return dao.searchAsnbh(asnbh, ckid);
        } catch (Exception e) {
            throw new ServiceException("ASN编号模糊查询失败", e);
        }
    }
    /**
     * 库位模糊查询
     * @param show_kwid 库位模糊查询
     * @return Page<WmsShelves>
     */
    public List<WmsLibraryPosition> searchKw(String show_kwid, String ckid, String hzid) throws RuntimeException {
        try {
            return dao.searchKw(show_kwid, ckid, hzid);
        } catch (Exception e) {
            throw new RuntimeException("ASN编号模糊查询失败", e);
        }
    }
    /**
     * 关联PO编号模糊查询
     * @param dycgdbh 关联PO编号模糊查询
     * @return Page<WmsShelves>
     */
    public List<WmsShelves> searchPObh(String dycgdbh, String ckid, String owner) throws ServiceException {
        try {
            return dao.searchPObh(dycgdbh, ckid, owner);
        } catch (Exception e) {
            throw new ServiceException("关联PO编号模糊查询失败", e);
        }
    }

    /**
     * 供应商模糊查询
     * @param wmsShelves
     * @param ckid
     * @param owner
     * @return
     * @throws ServiceException
     */
    public Page<WmsShelves> searchGysid(WmsShelves wmsShelves, String ckid, String owner) throws ServiceException {
        try {
            PageHelper.startPage(wmsShelves.getPage(), wmsShelves.getRows());
            return (Page<WmsShelves>) dao.searchGysid(wmsShelves.getShow_gysid(), ckid, owner);
        } catch (Exception e) {
            throw new ServiceException("供应商模糊查询失败", e);
        }
    }

    /**
     * 收货明细页面查询==>上架确认页面
     * 根据预期收货单ID,查询收货表信息
     * @param wmsShelves
     * @param ckid
     * @param hzid
     * @return
     * @throws ServiceException
     */
    @BaseServiceDescribe(moduleName = "上架管理", btnName = "上架确认查询")
    public Page<WmsShelves> baseSelectDetail(WmsShelves wmsShelves, String ckid, String hzid) throws ServiceException {

        //货主订单类型表:是否质检(0否 1是)
        String sfzj1 = "1";
        //0初始 1质检单创建 2部分质检 3全部质检 4 质检完成
        String zjzt2 = "4";
        //预期到货单明细表:质检状态(0初始 1质检单创建 2质检合格 3质检不合格)
        String zjzt = "2";
        //不取不合格数据
        String zjzt3 = "3";
        //货主表:质检方式(0不质检 1收货后质检 2上架后质检)
        String zjfs1 = "1";
        WmsOwner wmsOwner = dao.searchZjfs(hzid);
        //人工指定库位上架(取asn行表,收货表数据)
        String sjzzgz1 = TurnoverRules.sjzzgz1.getType();
        //系统按库位组指定上架(取上架表数据)==>库位高度不同,空库位优先
        String sjzzgz2 = TurnoverRules.sjzzgz2.getType();
        if (sjzzgz1.equals(wmsOwner.getShow_sjgzid())) {
            String sfzj2 = dao.searchDdlx(wmsShelves.getYqdhbdid(), hzid);
            //1收货后质检;1是质检
            if (zjfs1.equals(wmsOwner.getZjfs())) {
                if (sfzj1.equals(sfzj2)) {
                    WmsEstimatedArrivalOrder wmsEstimatedArrivalOrder = dao.searchYqdh(wmsShelves.getYqdhbdid(), zjzt2);
                    if (wmsEstimatedArrivalOrder != null) {
                        PageHelper.startPage(wmsShelves.getPage(), wmsShelves.getRows());
                        return (Page<WmsShelves>) dao.baseSelectDetail(wmsShelves.getYqdhbdid(), ckid, hzid, zjzt, zjzt3);
                    } else {
                        throw new ServiceException("请选择货品质检合格的收货单");
                    }
                } else {
                    PageHelper.startPage(wmsShelves.getPage(), wmsShelves.getRows());
                    return (Page<WmsShelves>) dao.baseSelectDetail(wmsShelves.getYqdhbdid(), ckid, hzid, "", zjzt3);
                }
            } else {
                PageHelper.startPage(wmsShelves.getPage(), wmsShelves.getRows());
                return (Page<WmsShelves>) dao.baseSelectDetail(wmsShelves.getYqdhbdid(), ckid, hzid, "", zjzt3);
            }
        } else if (sjzzgz2.equals(wmsOwner.getShow_sjgzid())) {
            String sfzj2 = dao.searchDdlx(wmsShelves.getYqdhbdid(), hzid);
            //1收货后质检;1是质检
            if (zjfs1.equals(wmsOwner.getZjfs())) {
                if (sfzj1.equals(sfzj2)) {
                    WmsEstimatedArrivalOrder wmsEstimatedArrivalOrder = dao.searchYqdh(wmsShelves.getYqdhbdid(), zjzt2);
                    if (wmsEstimatedArrivalOrder != null) {
                        PageHelper.startPage(wmsShelves.getPage(), wmsShelves.getRows());
                        return (Page<WmsShelves>) dao.baseSelectDetail2(wmsShelves.getYqdhbdid(), ckid, hzid, zjzt, zjzt3);
                    } else {
                        throw new ServiceException("请选择货品质检合格的收货单");
                    }
                } else {
                    PageHelper.startPage(wmsShelves.getPage(), wmsShelves.getRows());
                    return (Page<WmsShelves>) dao.baseSelectDetail2(wmsShelves.getYqdhbdid(), ckid, hzid, "", zjzt3);
                }
            } else {
                PageHelper.startPage(wmsShelves.getPage(), wmsShelves.getRows());
                return (Page<WmsShelves>) dao.baseSelectDetail2(wmsShelves.getYqdhbdid(), ckid, hzid, "", zjzt3);
            }
        } else {
            throw new ServiceException(wmsOwner.getShow_sjgzid() + "上架规则不可上架");
        }
    }
    /**
     * 上架任务
     * 收货明细页面查询==>上架确认页面
     * 根据预期收货单ID,查询收货表信息
     * @param wmsShelves
     * @param ckid
     * @param hzid
     * @return
     * @throws ServiceException
     */
    @BaseServiceDescribe(moduleName = "上架任务", btnName = "上架任务确认查询")
    public Page<WmsShelves> baseSelectRw(WmsShelves wmsShelves, String ckid, String hzid) throws ServiceException {
        //货主订单类型表:是否质检(0否 1是)
        String sfzj1 = "1";
        //0初始 1质检单创建 2部分质检 3全部质检 4 质检完成
        String zjzt = "4";
        //货主表:质检方式(0不质检 1收货后质检 2上架后质检)
        String zjfs1 = "1";
        //预期到货单明细表:质检状态(0初始 1质检单创建 2质检合格 3质检不合格)
        String zjzt3 = "3";
        WmsOwner wmsOwner = dao.searchZjfs(hzid);
        //人工指定库位上架(取asn行表,收货表数据)
        String sjzzgz1 = TurnoverRules.sjzzgz1.getType();
        //系统按库位组指定上架(取上架表数据)==>库位高度不同,空库位优先
        String sjzzgz2 = TurnoverRules.sjzzgz2.getType();
        if (sjzzgz1.equals(wmsOwner.getShow_sjgzid())) {
            //人工指定库位上架(取asn行表,收货表数据)
            String sfzj2 = dao.searchDdlx(wmsShelves.getYqdhbdid(), hzid);
            //1收货后质检;
            if (zjfs1.equals(wmsOwner.getZjfs())) {
                //1是质检
                if (sfzj1.equals(sfzj2)) {
                    WmsEstimatedArrivalOrder wmsEstimatedArrivalOrder = dao.searchYqdh(wmsShelves.getYqdhbdid(), zjzt);
                    if (wmsEstimatedArrivalOrder != null) {
                        PageHelper.startPage(wmsShelves.getPage(), wmsShelves.getRows());
                        return (Page<WmsShelves>) dao.baseSelectRw(wmsShelves.getYqdhbdid(), ckid, hzid, zjzt, zjzt3);
                    } else {
                        throw new ServiceException("请选择货品质检合格以及全部收货的信息");
                    }
                } else {
                    //不质检
                    PageHelper.startPage(wmsShelves.getPage(), wmsShelves.getRows());
                    return (Page<WmsShelves>) dao.baseSelectRw(wmsShelves.getYqdhbdid(), ckid, hzid, "", zjzt3);
                }
            } else {
                PageHelper.startPage(wmsShelves.getPage(), wmsShelves.getRows());
                return (Page<WmsShelves>) dao.baseSelectRw(wmsShelves.getYqdhbdid(), ckid, hzid, "", zjzt3);
            }
        } else if (sjzzgz2.equals(wmsOwner.getShow_sjgzid())) {
            //系统按库位组指定上架(取上架表数据)==>库位高度不同,空库位优先
            String sfzj2 = dao.searchDdlx(wmsShelves.getYqdhbdid(), hzid);
            //1收货后质检;
            if (zjfs1.equals(wmsOwner.getZjfs())) {
                //1是质检
                if (sfzj1.equals(sfzj2)) {
                    WmsEstimatedArrivalOrder wmsEstimatedArrivalOrder = dao.searchYqdh(wmsShelves.getYqdhbdid(), zjzt);
                    if (wmsEstimatedArrivalOrder != null) {
                        PageHelper.startPage(wmsShelves.getPage(), wmsShelves.getRows());
                        return (Page<WmsShelves>) dao.baseSelectRw2(wmsShelves.getYqdhbdid(), ckid, hzid, zjzt, zjzt3);
                    } else {
                        throw new ServiceException("请选择货品质检合格以及全部收货的信息");
                    }
                } else {
                    //不质检
                    PageHelper.startPage(wmsShelves.getPage(), wmsShelves.getRows());
                    return (Page<WmsShelves>) dao.baseSelectRw2(wmsShelves.getYqdhbdid(), ckid, hzid, "", zjzt3);
                }
            } else {
                PageHelper.startPage(wmsShelves.getPage(), wmsShelves.getRows());
                return (Page<WmsShelves>) dao.baseSelectRw2(wmsShelves.getYqdhbdid(), ckid, hzid, "", zjzt3);
            }
        } else {
            throw new ServiceException(wmsOwner.getShow_sjgzid() + "上架规则不可上架");
        }
    }

    /**
     * 判断库位是否可用
     *
     * @param wmsShelves
     * @return
     */
    @Transactional
    public Boolean judge(WmsShelves wmsShelves) throws Exception {
        Boolean flag = false;
        //查询库位是否绑定库位组
        List<WmsLibrarypositiongroup> wmsLibrarypositiongroupList = dao.searchKwz(null, wmsShelves.getKwid());
        if (wmsLibrarypositiongroupList.size() > 0) {
            //查询货品是否绑定该库位组
            List<WmsGoods> wmsGoodsList = dao.searchHp(wmsLibrarypositiongroupList, wmsShelves.getShow_bh());
            if (wmsGoodsList.size() > 0) {
                flag = true;
            } else {
                flag = false;
                List<WmsLibrarypositiongroup> wmsLibrarypositiongroupList1 = new ArrayList<>();
                List<WmsGoods> wmsGoods1 = dao.searchHp(wmsLibrarypositiongroupList1, wmsShelves.getShow_bh());
                throw new RuntimeException(wmsShelves.getShow_kwid() + "库位已绑定库位组," + wmsGoods1.get(0).getMc() + "不可上架");
            }
        } else {
            //根据货品编号查询是否绑定库位组
            List<WmsGoods> wmsGoodsList = dao.searchHp(wmsLibrarypositiongroupList, wmsShelves.getShow_bh());
            if (wmsGoodsList.size() > 0) {
                for (WmsGoods wmsGoods : wmsGoodsList) {
                    //未绑定库位组
                    if (!"".equals(wmsGoods.getGlkwzid()) && wmsGoods.getGlkwzid() != null) {
                        //查询库位是否绑定库位组
                        List<WmsLibrarypositiongroup> wmsLibrarypositiongroupList2 = dao.searchKwz(wmsGoods.getGlkwzid(), wmsShelves.getKwid());
                        if (wmsLibrarypositiongroupList2.size() > 0) {
                            flag = true;
                        }
                    } else {
                        flag = true;
                    }
                }
                if (!flag) {
                    throw new RuntimeException("货品编号:" + wmsShelves.getShow_bh() + "已绑定库位组," + wmsShelves.getShow_kwid() + "库位不可上架");
                }
            } else {
                flag = false;
                throw new RuntimeException("上架货品有误");
            }
        }
        return flag;
    }

    /**
     * 收货明细页面查询==>上架确认页面
     * 根据预期收货单ID,查询收货表信息
     * @param list
     * @return Integer
     */
    @BaseServiceDescribe(moduleName = "上架管理", btnName = "上架确认保存")
    @Transactional
    public void baseInsert(List<WmsShelves> list, String ckid, String hzid, String sign) throws Exception {
        String yqdhbdid=list.get(0).getYqdhbdid();
        //需要前台传预期到货单表ID(yqdhbdid)
        //dao.deleteSj(yqdhbdid);
        if (list.size() > 0 && list.get(0) != null) {
            List<WmsReceiving> wmsReceivingList = dao.searchReceiving3(list);
            if (wmsReceivingList.size() > 0) {
                throw new RuntimeException("上架信息有误,请退出刷新页面");
            }
        }
        for(WmsShelves wmsShelves: list) {
            Boolean flag = judge(wmsShelves);
            Boolean flag2 = false;
            if (flag) {
                WmsLibraryPosition wmsLibraryPosition = dao.searchKwHphf(wmsShelves.getKwid());
                //查询库位对应的库存内的货品
                List<WmsStock> wmsStockList = dao.searchHpKc(wmsShelves.getKwid());
                Integer k2 = wmsStockList.size();
                if (k2 > 0) {
                    for (WmsStock wmsStock : wmsStockList) {
                        //判断货品相同是否可以货品混放 0不可 1允许(hphf)
                        String show_bh = wmsShelves.getShow_bh();
                        if (show_bh.equals(wmsStock.getHpbh())) {
                            //判断同货品批次是否相同
                            String hpPc1 = wmsShelves.getShow_hppc();
                            String hpPc2 = wmsStock.getHppc();
                            if (hpPc1.equals(hpPc2)) {
                                flag2 = true;
                                //货品以及批次都相同
                                //addFZ(wmsShelves, ckid, hzid, wmsStockList1, wmsStockList2, wmsShelvesList, wmsReceivingsList,"0");
                            } else {
                                //批次不相同 同货品批次是否可以混放 0不可 1允许(pchf)
                                String pcHf = "0";
                                if (pcHf.equals(wmsLibraryPosition.getPchf())) {
                                    //rollBack(wmsStockList1, wmsStockList2, wmsShelvesList, wmsReceivingsList);
                                    throw new RuntimeException(wmsShelves.getShow_kwid() + "库位同货品不同批次不可以混放");
                                } else {
                                    //货品相同,批次不相同
                                    flag2 = true;
                                    //addFZ(wmsShelves, ckid, hzid, wmsStockList1, wmsStockList2, wmsShelvesList, wmsReceivingsList,"0");
                                }
                            }
                        } else {
                            //货品不相同 是否可以货品混放 0不可 1允许(hphf)
                            if ("0".equals(wmsLibraryPosition.getHphf())) {
                                //rollBack(wmsStockList1, wmsStockList2, wmsShelvesList, wmsReceivingsList);
                                throw new RuntimeException(wmsShelves.getShow_kwid() + "库位不同货品不可以混放");
                            } else {
                                //判断同货品批次是否相同
                                String show_hppc = wmsShelves.getShow_hppc();
                                if (show_hppc.equals(wmsStock.getHppc())) {
                                    //货品以及批次都不相同
                                    flag2 = true;
                                    //addFZ(wmsShelves, ckid, hzid, wmsStockList1, wmsStockList2, wmsShelvesList, wmsReceivingsList,"0");
                                } else {
                                    //批次不相同 同货品批次是否可以混放 0不可 1允许(pchf)
                                    if ("0".equals(wmsLibraryPosition.getPchf())) {
                                        //rollBack(wmsStockList1, wmsStockList2, wmsShelvesList, wmsReceivingsList);
                                        throw new RuntimeException(wmsShelves.getShow_kwid() + "库位同货品不同批次不可以混放");
                                    } else {
                                        //货品不相同,批次不相同
                                        flag2 = true;
                                        //addFZ(wmsShelves, ckid, hzid, wmsStockList1, wmsStockList2, wmsShelvesList, wmsReceivingsList,"0");
                                    }
                                }
                            }
                        }
                    }
                    if (flag2) {
                        addFZ(wmsShelves, ckid, hzid, sign);
                    }
                } else {
                    //库位内没有库存数据
                    addFZ(wmsShelves, ckid, hzid, sign);
                }
            }
            /*}*/
        }
        //更改预期到货通知单上架时间
        if (list.size() > 0) {
            dao.updateSjsj(list.get(0).getYqdhbdid());
        }
    }

    /**
     * 添加辅助代码
     * @param wmsShelves
     * @param ckid
     * @param hzid
     * @param  sign   标记  lh专用
     * @throws RuntimeException
     */
    @Transactional
    public void addFZ(WmsShelves wmsShelves, String ckid, String hzid, String sign) throws RuntimeException {
        Integer i=0;
        //根据id查询库存表
        WmsStock wmsStock=dao.searchStock(wmsShelves.getKcid());
        wmsStock.setId(UUIDUtils.uuid());
        if (wmsShelves.getSjsl() != null && !"".equals(wmsShelves.getSjsl())) {
            wmsStock.setSl(wmsShelves.getSjsl());
        }
        /*if (wmsShelves.getSl() != null && !"".equals(wmsShelves.getSl())) {
            wmsStock.setSl(wmsShelves.getSl());
            wmsShelves.setSjsl(wmsShelves.getSl());
        }*/
        wmsStock.setKwid(wmsShelves.getKwid());
        wmsStock.setCkid(ckid);
        wmsStock.setSshzid(hzid);
        i=dao.insertWms_stock(wmsStock);//保存库存信息
        if(i>0){
            log.info("上架操作(库存id:" + wmsStock.getId() + ";库存数量:" + wmsStock.getSl() + ")");
            //库位更改状态已被占用
            i = dao.updateStockZt(wmsShelves.getKwid(), "1");
            if (i > 0) {
                String kcid = wmsShelves.getKcid();
                wmsShelves.setId(UUIDUtils.uuid());
                wmsShelves.setKcid(wmsStock.getId());
                wmsShelves.setCkid(ckid);
                //保存上架信息
                i = dao.baseInsert(wmsShelves);
                if (i > 0) {
                    WmsReceiving wmsReceiving = dao.searchReceiving(wmsShelves.getSsshid());
                    if (wmsReceiving != null) {
                        //修改收货表上架状态为已上架(已上架,已分配,页面)
                        i = dao.updateWms_receiving(wmsShelves.getSsshid(), "1", "", "0");
                    } else {
                        i = 0;
                    }
                    if (i > 0) {
                        WmsStock wmsStock1 = dao.searchStock(kcid);
                        if(sign=="0"){//标识为“0”时 为不合格品处理中的上架 lh专用
                            //原库存的数量减去上架的数量
                            Integer sl = Integer.parseInt(wmsStock1.getSl()) - Integer.parseInt(wmsShelves.getSjsl());
                            i = dao.updateWms_stock(kcid, sl.toString());
                        }else {
                            i = dao.updateWms_stock(kcid, "0");
                        }

                        if (i > 0) {
                        } else {
                            //rollBack(wmsStockList1, wmsStockList2, wmsShelvesList, wmsReceivingsList);
                            throw new RuntimeException("收货库存保存失败");
                        }
                    } else {
                        //rollBack(wmsStockList1, wmsStockList2, wmsShelvesList, wmsReceivingsList);
                        throw new RuntimeException("上架状态更改失败");
                    }
                } else {
                    //rollBack(wmsStockList1, wmsStockList2, wmsShelvesList, wmsReceivingsList);
                    throw new RuntimeException("上架确认保存失败");
                }
            }
        }else{
            //rollBack(wmsStockList1,wmsStockList2,wmsShelvesList,wmsReceivingsList);
            throw new RuntimeException("上架确认保存失败");
        }
    }

    /**
     * 回滚辅助方法
     * @param wmsStockList1(存储上架生成库存信息)
     * @param wmsStockList2(存储更改收货库存信息)
     * @param wmsShelvesList(存储上架保存信息)
     * @param wmsReceivingsList(存储收货表更改信息)
     */
    public void rollBack(List<WmsStock> wmsStockList1, List<WmsStock> wmsStockList2, List<WmsShelves> wmsShelvesList, List<WmsReceiving> wmsReceivingsList){
        //回滚新生成的库存
        for(WmsStock wmsStock : wmsStockList1){
            dao.updateStockZt(wmsStock.getKwid(), "0");
            dao.deleteKc(wmsStock);
        }
        //回滚收货库存
        for(WmsStock wmsStock : wmsStockList2){
            dao.updateWms_stock2(wmsStock);
        }
        //回滚上架信息
        for(WmsShelves wmsShelves : wmsShelvesList){
            dao.deleteSj2(wmsShelves);
        }
        //回滚收货信息
        for(WmsReceiving wmsReceiving : wmsReceivingsList){
            wmsReceiving.setSjzt("0");
            dao.updateWms_receiving(wmsReceiving.getId(), wmsReceiving.getSjzt(), "", wmsReceiving.getCzfs());
        }
    }

    /**
     * 新增上架任务
     * @param list
     * @param ckid
     * @param hzid
     * @param user
     * @throws Exception
     */
    @BaseServiceDescribe(moduleName = "上架管理", btnName = "上架任务确认保存")
    @Transactional
    public void addRW(List<WmsShelves> list, String ckid, String hzid, User user) throws Exception {
        //PDA上架表明细更改信息(用于回滚)
        List<Integer> wmsShelvesTaskPdaDetailedList = new ArrayList<Integer>();
        //存储收货表更改信息(用于回滚)
        List<WmsReceiving> wmsReceivingsList = new ArrayList<WmsReceiving>();
        //pda消息推送更改信息(用于回滚)
        List<WmsPdaMsg> wmsPdaMsgList = new ArrayList<WmsPdaMsg>();
        WmsReceiving wmsReceiving = null;
        WmsShelvesTaskPda wmsShelvesTaskPda2 = null;
        WmsShelvesTaskPdaDetailed wmsShelvesTaskPdaDetailed = null;
        WmsPdaMsg wmsPdaMsg = null;
        //查询预期
        //查询当前货主信息
        WmsOwner wmsOwner = dao.searchZjfs(hzid);
        //货主订单类型表:是否质检(0否 1是)
        String sfzj1 = "1";
        //预期到货单明细表:质检状态(0初始 1质检单创建 2质检合格 3质检不合格)
        String zjzt = null;
        if (list.size() > 0) {
            for (WmsShelves wmsShelves1 : list) {
                List<WmsReceiving> wmsReceivingList4 = dao.searchReceiving4(wmsShelves1.getYqdhbdid());
                if (wmsReceivingList4.size() > 0 && wmsReceivingList4.get(0) != null) {
                    String sfzj2 = dao.searchDdlx(wmsShelves1.getYqdhbdid(), hzid);
                    if (sfzj1.equals(wmsOwner.getZjfs())) {
                        if (sfzj1.equals(sfzj2)) {
                            zjzt = "2";
                            //根据预期到货通知单ID查询数据
                            WmsEstimatedArrivalOrder wmsEstimatedArrivalOrder = dao.searchYqdhbd(wmsShelves1.getYqdhbdid());
                            //查询生成上架任务的数据
                            List<WmsReceiving> wmsReceivingList = dao.searchReceiving2(wmsShelves1.getYqdhbdid(), "0", zjzt, null);
                            if (wmsReceivingList.size() > 0) {
                                //查询ans单号在pda上架表内是否上架
                                WmsShelvesTaskPda wmsShelvesTaskPda = dao.searchPda(wmsEstimatedArrivalOrder.getAsnbh());
                                if (wmsShelvesTaskPda != null) {
                                    //rollBack2(wmsShelvesTaskPda2, wmsShelvesTaskPdaDetailedList, wmsReceivingsList);
                                    throw new RuntimeException(wmsEstimatedArrivalOrder.getAsnbh() + "订单号,PDA正在上架,需PDA作业完毕后可生成上架任务");
                                } else {
                                    wmsShelvesTaskPda2 = new WmsShelvesTaskPda();
                                    //添加PDA上架表数据
                                    Integer i = getInteger(ckid, hzid, user, wmsShelvesTaskPda2, wmsOwner, wmsShelves1, wmsEstimatedArrivalOrder);
                                    if (i > 0) {
                                        //查询ans单号在pda上架表ID
                                        WmsShelvesTaskPda wmsShelvesTaskPda1 = dao.searchPda(wmsShelvesTaskPda2.getYqdh());
                                        List<WmsShelves> list2 = dao.selectRw(wmsShelves1.getYqdhbdid(), hzid, ckid, zjzt, null);
                                        for (WmsShelves wmsShelves : list2) {
                                            wmsShelvesTaskPdaDetailed = new WmsShelvesTaskPdaDetailed();
                                            //添加PDA上架表明细数据
                                            i = getIntegerDetailed(wmsShelvesTaskPdaDetailed, wmsEstimatedArrivalOrder, wmsShelvesTaskPda1, wmsShelves);
                                            if (i > 0) {
                                                wmsShelvesTaskPdaDetailedList.add(wmsShelvesTaskPdaDetailed.getId());
                                                i = dao.updateWms_receiving(wmsShelves.getSsshid(), "", "1", "1");
                                                if (i > 0) {
                                                } else {
                                                    //rollBack2(wmsShelvesTaskPda2, wmsShelvesTaskPdaDetailedList, wmsReceivingsList);
                                                    throw new RuntimeException("收货表生成状态更改失败");
                                                }
                                            } else {
                                                //rollBack2(wmsShelvesTaskPda2, wmsShelvesTaskPdaDetailedList, wmsReceivingsList);
                                                throw new RuntimeException("PDA上架任务明细生成失败");
                                            }
                                        }
                                    } else {
                                        //rollBack2(wmsShelvesTaskPda2, wmsShelvesTaskPdaDetailedList, wmsReceivingsList);
                                        throw new RuntimeException("PDA上架任务生成失败");
                                    }
                                }
                                //更改预期到货通知单生成上架任务状态
                                dao.updateSjRw(wmsShelves1.getYqdhbdid(), "1");
                                //PDA消息推送
                                saveMsg(wmsPdaMsg, wmsEstimatedArrivalOrder, wmsShelves1);
                            } else {
                                //rollBack2(wmsShelvesTaskPda2, wmsShelvesTaskPdaDetailedList, wmsReceivingsList);
                                throw new RuntimeException(wmsEstimatedArrivalOrder.getAsnbh() + "订单号,无生成上架任务数据");
                            }
                        } else {
                            zjzt = "3";
                            //根据预期到货通知单ID查询数据
                            WmsEstimatedArrivalOrder wmsEstimatedArrivalOrder = dao.searchYqdhbd(wmsShelves1.getYqdhbdid());
                            //查询生成上架任务的数据
                            List<WmsReceiving> wmsReceivingList = dao.searchReceiving2(wmsShelves1.getYqdhbdid(), "0", null, zjzt);
                            if (wmsReceivingList.size() > 0) {
                                //查询ans单号在pda上架表内是否上架
                                WmsShelvesTaskPda wmsShelvesTaskPda = dao.searchPda(wmsEstimatedArrivalOrder.getAsnbh());
                                if (wmsShelvesTaskPda != null) {
                                    //rollBack2(wmsShelvesTaskPda2, wmsShelvesTaskPdaDetailedList, wmsReceivingsList);
                                    throw new RuntimeException(wmsEstimatedArrivalOrder.getAsnbh() + "订单号,PDA正在上架,需PDA作业完毕后可生成上架任务");
                                } else {
                                    wmsShelvesTaskPda2 = new WmsShelvesTaskPda();
                                    //添加PDA上架表数据
                                    Integer i = getInteger(ckid, hzid, user, wmsShelvesTaskPda2, wmsOwner, wmsShelves1, wmsEstimatedArrivalOrder);
                                    if (i > 0) {
                                        //查询ans单号在pda上架表ID
                                        WmsShelvesTaskPda wmsShelvesTaskPda1 = dao.searchPda(wmsShelvesTaskPda2.getYqdh());
                                        List<WmsShelves> list2 = dao.selectRw(wmsShelves1.getYqdhbdid(), hzid, ckid, null, zjzt);
                                        for (WmsShelves wmsShelves : list2) {
                                            wmsShelvesTaskPdaDetailed = new WmsShelvesTaskPdaDetailed();
                                            //添加PDA上架表明细数据
                                            i = getIntegerDetailed(wmsShelvesTaskPdaDetailed, wmsEstimatedArrivalOrder, wmsShelvesTaskPda1, wmsShelves);
                                            if (i > 0) {
                                                wmsShelvesTaskPdaDetailedList.add(wmsShelvesTaskPdaDetailed.getId());
                                                i = dao.updateWms_receiving(wmsShelves.getSsshid(), "", "1", "1");
                                                if (i > 0) {
                                                } else {
                                                    //rollBack2(wmsShelvesTaskPda2, wmsShelvesTaskPdaDetailedList, wmsReceivingsList);
                                                    throw new RuntimeException("收货表生成状态更改失败");
                                                }
                                            } else {
                                                //rollBack2(wmsShelvesTaskPda2, wmsShelvesTaskPdaDetailedList, wmsReceivingsList);
                                                throw new RuntimeException("PDA上架任务明细生成失败");
                                            }
                                        }
                                    } else {
                                        //rollBack2(wmsShelvesTaskPda2, wmsShelvesTaskPdaDetailedList, wmsReceivingsList);
                                        throw new RuntimeException("PDA上架任务生成失败");
                                    }
                                }
                                //更改预期到货通知单生成上架任务状态
                                dao.updateSjRw(wmsShelves1.getYqdhbdid(), "1");
                                //PDA消息推送
                                saveMsg(wmsPdaMsg, wmsEstimatedArrivalOrder, wmsShelves1);
                            } else {
                                //rollBack2(wmsShelvesTaskPda2, wmsShelvesTaskPdaDetailedList, wmsReceivingsList);
                                throw new RuntimeException(wmsEstimatedArrivalOrder.getAsnbh() + "订单号,无生成上架任务数据");
                            }
                        }
                    } else {
                        zjzt = "3";
                        //根据预期到货通知单ID查询数据
                        WmsEstimatedArrivalOrder wmsEstimatedArrivalOrder = dao.searchYqdhbd(wmsShelves1.getYqdhbdid());
                        //查询生成上架任务的数据
                        List<WmsReceiving> wmsReceivingList = dao.searchReceiving2(wmsShelves1.getYqdhbdid(), "0", null, zjzt);
                        if (wmsReceivingList.size() > 0) {
                            //查询ans单号在pda上架表内是否上架
                            WmsShelvesTaskPda wmsShelvesTaskPda = dao.searchPda(wmsEstimatedArrivalOrder.getAsnbh());
                            if (wmsShelvesTaskPda != null) {
                                //rollBack2(wmsShelvesTaskPda2, wmsShelvesTaskPdaDetailedList, wmsReceivingsList);
                                throw new RuntimeException(wmsEstimatedArrivalOrder.getAsnbh() + "订单号,PDA正在上架,需PDA作业完毕后可生成上架任务");
                            } else {
                                wmsShelvesTaskPda2 = new WmsShelvesTaskPda();
                                //添加PDA上架表数据
                                Integer i = getInteger(ckid, hzid, user, wmsShelvesTaskPda2, wmsOwner, wmsShelves1, wmsEstimatedArrivalOrder);
                                if (i > 0) {
                                    //查询ans单号在pda上架表ID
                                    WmsShelvesTaskPda wmsShelvesTaskPda1 = dao.searchPda(wmsShelvesTaskPda2.getYqdh());
                                    List<WmsShelves> list2 = dao.selectRw(wmsShelves1.getYqdhbdid(), hzid, ckid, null, zjzt);
                                    for (WmsShelves wmsShelves : list2) {
                                        wmsShelvesTaskPdaDetailed = new WmsShelvesTaskPdaDetailed();
                                        //添加PDA上架表明细数据
                                        i = getIntegerDetailed(wmsShelvesTaskPdaDetailed, wmsEstimatedArrivalOrder, wmsShelvesTaskPda1, wmsShelves);
                                        if (i > 0) {
                                            wmsShelvesTaskPdaDetailedList.add(wmsShelvesTaskPdaDetailed.getId());
                                            i = dao.updateWms_receiving(wmsShelves.getSsshid(), "", "1", "1");
                                            if (i > 0) {
                                            } else {
                                                //rollBack2(wmsShelvesTaskPda2, wmsShelvesTaskPdaDetailedList, wmsReceivingsList);
                                                throw new RuntimeException("收货表生成状态更改失败");
                                            }
                                        } else {
                                            //rollBack2(wmsShelvesTaskPda2, wmsShelvesTaskPdaDetailedList, wmsReceivingsList);
                                            throw new RuntimeException("PDA上架任务明细生成失败");
                                        }
                                    }
                                } else {
                                    //rollBack2(wmsShelvesTaskPda2, wmsShelvesTaskPdaDetailedList, wmsReceivingsList);
                                    throw new RuntimeException("PDA上架任务生成失败");
                                }
                            }
                            //更改预期到货通知单生成上架任务状态
                            dao.updateSjRw(wmsShelves1.getYqdhbdid(), "1");
                            //PDA消息推送
                            saveMsg(wmsPdaMsg, wmsEstimatedArrivalOrder, wmsShelves1);
                        } else {
                            //rollBack2(wmsShelvesTaskPda2, wmsShelvesTaskPdaDetailedList, wmsReceivingsList);
                            throw new RuntimeException(wmsEstimatedArrivalOrder.getAsnbh() + "订单号,无生成上架任务数据");
                        }
                    }
                } else {
                    throw new RuntimeException("该条数据无上架任务可发送,请刷新页面");
                }
            }
        } else {
            //rollBack2(wmsShelvesTaskPda2, wmsShelvesTaskPdaDetailedList, wmsReceivingsList);
            throw new RuntimeException("请至少选择一条数据");
        }
    }

    /**
     * pda数据明细添加
     *
     * @param wmsShelvesTaskPdaDetailed
     * @param wmsEstimatedArrivalOrder
     * @param wmsShelvesTaskPda1
     * @param wmsShelves
     * @return
     * @throws ServiceException
     */
    public Integer getIntegerDetailed(WmsShelvesTaskPdaDetailed wmsShelvesTaskPdaDetailed, WmsEstimatedArrivalOrder wmsEstimatedArrivalOrder, WmsShelvesTaskPda wmsShelvesTaskPda1, WmsShelves wmsShelves) throws ServiceException {
        Integer i;//所属PDA上架ID
        wmsShelvesTaskPdaDetailed.setPdasjid(String.valueOf(wmsShelvesTaskPda1.getId()));
        //预期到货明细ID
        wmsShelvesTaskPdaDetailed.setYqdhmxid(wmsShelves.getYqdhbdmxid());
        //所属收货ID
        wmsShelvesTaskPdaDetailed.setSsshid(wmsShelves.getSsshid());
        //货品名称
        wmsShelvesTaskPdaDetailed.setHpmc(wmsShelves.getShow_mc());
        //货品编号
        wmsShelvesTaskPdaDetailed.setHpbh(wmsShelves.getShow_bh());
        //货品批次
        wmsShelvesTaskPdaDetailed.setPc(wmsShelves.getShow_hppc());
        //生产日期
        wmsShelvesTaskPdaDetailed.setScrq(wmsShelves.getShow_scrq());
        //供应商名称
        wmsShelvesTaskPdaDetailed.setGys(wmsEstimatedArrivalOrder.getGysmc());
        //应上架主单位数量
        wmsShelvesTaskPdaDetailed.setSl(wmsShelves.getSl());
        //主单位名称
        wmsShelvesTaskPdaDetailed.setZdwmc(wmsShelves.getZdwmc());
        //箱单位名称
        wmsShelvesTaskPdaDetailed.setXdwmc(wmsShelves.getXdwmc());
        //主、箱换算量
        wmsShelvesTaskPdaDetailed.setHsl(wmsShelves.getZdwsl());
        //对应库存ID
        wmsShelvesTaskPdaDetailed.setDykcid(wmsShelves.getKcid());
        //对应库位名称
        if (wmsShelves.getShow_kwid() != null) {
            wmsShelvesTaskPdaDetailed.setKwmc(wmsShelves.getShow_kwid());
        } else {
            wmsShelvesTaskPdaDetailed.setKwmc("");
        }
        //规格
        if (wmsShelves.getGg() != null) {
            wmsShelvesTaskPdaDetailed.setGg(wmsShelves.getGg());
        } else {
            wmsShelvesTaskPdaDetailed.setGg("");
        }
        //LPN码
        if (!"".equals(wmsShelves.getLpn()) && wmsShelves.getLpn() != null) {
            wmsShelvesTaskPdaDetailed.setLpn(wmsShelves.getLpn());
        } else {
            wmsShelvesTaskPdaDetailed.setLpn("0");
        }
        String th = null;
        if (!"".equals(wmsShelves.getKwid()) && wmsShelves.getKwid() != null) {
            th = wmsPalletSortingService.th(wmsShelves.getKwid(), wmsShelves.getHpid());
        } else {
            th = wmsShelves.getTh();
        }
        //TI*HI
        wmsShelvesTaskPdaDetailed.setTh(th);

        //货品id
        wmsShelvesTaskPdaDetailed.setHpid(wmsShelves.getHpid());
        //收货库位名称
        wmsShelvesTaskPdaDetailed.setShkwmc(wmsShelves.getShkwmc());
        //添加PDA上架明细
        i = dao.insertShelvesPdaDetailed(wmsShelvesTaskPdaDetailed);
        return i;
    }

    /**
     * 添加PDA上架表数据
     *
     * @param ckid
     * @param hzid
     * @param user
     * @param wmsShelvesTaskPda2
     * @param wmsOwner
     * @param wmsShelves1
     * @param wmsEstimatedArrivalOrder
     * @return
     */
    public Integer getInteger(String ckid, String hzid, User user, WmsShelvesTaskPda wmsShelvesTaskPda2, WmsOwner wmsOwner, WmsShelves wmsShelves1, WmsEstimatedArrivalOrder wmsEstimatedArrivalOrder) {
        //货主ID
        wmsShelvesTaskPda2.setSshzid(hzid);
        //预期到货单ID
        wmsShelvesTaskPda2.setYqid(wmsEstimatedArrivalOrder.getId());
        //预期单号
        wmsShelvesTaskPda2.setYqdh(wmsEstimatedArrivalOrder.getAsnbh());
        //分配人员ID
        wmsShelvesTaskPda2.setRyid("," + wmsShelves1.getCzrId() + ",");
        //创建人
        wmsShelvesTaskPda2.setCjr(user.getName());
        //上架类型 收货类型 0常规 1码盘
        wmsShelvesTaskPda2.setSjlx(wmsEstimatedArrivalOrder.getMpzt());
        //仓库ID
        wmsShelvesTaskPda2.setCkid(ckid);
        //优先级
        wmsShelvesTaskPda2.setYxj(wmsEstimatedArrivalOrder.getYxj());
        //按照优先级作业 0：否 1：是
        wmsShelvesTaskPda2.setBxyxj(wmsOwner.getYxj());
        //供应商名称
        wmsShelvesTaskPda2.setGysmc(wmsEstimatedArrivalOrder.getGysmc());
        //扫描货品码 0否 1是
        wmsShelvesTaskPda2.setCmhpm(wmsOwner.getCmhpm());
        //添加PDA上架表数据
        return dao.insertShelvesPda(wmsShelvesTaskPda2);
    }


    /**
     * 上架任务添加辅助
     *
     * @param wmsShelvesTaskPda2
     * @param wmsEstimatedArrivalOrder
     * @param wmsShelves1
     * @param wmsOwner
     * @param ckid
     * @param user
     * @return
     */
    @Transactional
    public Integer addRwFz(WmsShelvesTaskPda wmsShelvesTaskPda2, WmsEstimatedArrivalOrder wmsEstimatedArrivalOrder, WmsShelves wmsShelves1, WmsOwner wmsOwner, String ckid, User user) {
        Integer i = 0;
        //查询预期
        wmsShelvesTaskPda2 = new WmsShelvesTaskPda();
        //货主ID
        wmsShelvesTaskPda2.setSshzid(wmsOwner.getId());
        //预期到货单ID
        wmsShelvesTaskPda2.setYqid(wmsEstimatedArrivalOrder.getId());
        //预期单号
        wmsShelvesTaskPda2.setYqdh(wmsEstimatedArrivalOrder.getAsnbh());
        //分配人员ID
        wmsShelvesTaskPda2.setRyid("," + wmsShelves1.getCzrId() + ",");
        //创建人
        wmsShelvesTaskPda2.setCjr(user.getName());
        //上架类型 收货类型 0常规 1码盘
        wmsShelvesTaskPda2.setSjlx(wmsEstimatedArrivalOrder.getMpzt());
        //仓库ID
        wmsShelvesTaskPda2.setCkid(ckid);
        //优先级
        wmsShelvesTaskPda2.setYxj(wmsEstimatedArrivalOrder.getYxj());
        //按照优先级作业 0：否 1：是
        wmsShelvesTaskPda2.setBxyxj(wmsOwner.getYxj());
        //供应商名称
        wmsShelvesTaskPda2.setGysmc(wmsEstimatedArrivalOrder.getGysmc());
        //扫描货品码 0否 1是
        wmsShelvesTaskPda2.setCmhpm(wmsOwner.getCmhpm());
        //添加PDA上架表数据
        i = dao.insertShelvesPda(wmsShelvesTaskPda2);
        return i;
    }

    /**
     * 添加上架任务明细辅助
     *
     * @param wmsShelvesTaskPda1
     * @param list2
     * @param gysmc
     * @param wmsShelvesTaskPdaDetailed
     * @throws Exception
     */
    @Transactional
    public void addRwMxFz(WmsShelvesTaskPda wmsShelvesTaskPda1, List<WmsShelves> list2, String gysmc, WmsShelvesTaskPdaDetailed wmsShelvesTaskPdaDetailed) throws Exception {
        Integer i = 0;
        for (WmsShelves wmsShelves : list2) {
            wmsShelvesTaskPdaDetailed = new WmsShelvesTaskPdaDetailed();
            //所属PDA上架ID
            wmsShelvesTaskPdaDetailed.setPdasjid(String.valueOf(wmsShelvesTaskPda1.getId()));
            //预期到货明细ID
            wmsShelvesTaskPdaDetailed.setYqdhmxid(wmsShelves.getYqdhbdmxid());
            //所属收货ID
            wmsShelvesTaskPdaDetailed.setSsshid(wmsShelves.getSsshid());
            //货品名称
            wmsShelvesTaskPdaDetailed.setHpmc(wmsShelves.getShow_mc());
            //货品编号
            wmsShelvesTaskPdaDetailed.setHpbh(wmsShelves.getShow_bh());
            //货品批次
            wmsShelvesTaskPdaDetailed.setPc(wmsShelves.getShow_hppc());
            //生产日期
            wmsShelvesTaskPdaDetailed.setScrq(wmsShelves.getShow_scrq());
            //供应商名称
            wmsShelvesTaskPdaDetailed.setGys(gysmc);
            //应上架主单位数量
            wmsShelvesTaskPdaDetailed.setSl(wmsShelves.getSl());
            //主单位名称
            wmsShelvesTaskPdaDetailed.setZdwmc(wmsShelves.getZdwmc());
            //箱单位名称
            wmsShelvesTaskPdaDetailed.setXdwmc(wmsShelves.getXdwmc());
            //主、箱换算量
            wmsShelvesTaskPdaDetailed.setHsl(wmsShelves.getZdwsl());
            //对应库存ID
            wmsShelvesTaskPdaDetailed.setDykcid(wmsShelves.getKcid());
            //对应库位名称
            if (wmsShelves.getShow_kwid() != null) {
                wmsShelvesTaskPdaDetailed.setKwmc(wmsShelves.getShow_kwid());
                String th = wmsPalletSortingService.th(wmsShelves.getKwid(), wmsShelves.getHpid());
                wmsShelvesTaskPdaDetailed.setTh(th);
            } else {
                //TI*HI
                if (!"".equals(wmsShelves.getTh()) && wmsShelves.getTh() != null) {
                    wmsShelvesTaskPdaDetailed.setTh(wmsShelves.getTh());
                } else {
                    wmsShelvesTaskPdaDetailed.setTh("");
                }
                wmsShelvesTaskPdaDetailed.setKwmc("");
            }
            //规格
            if (wmsShelves.getGg() != null) {
                wmsShelvesTaskPdaDetailed.setGg(wmsShelves.getGg());
            } else {
                wmsShelvesTaskPdaDetailed.setGg("");
            }
            //LPN码
            if (!"".equals(wmsShelves.getLpn()) && wmsShelves.getLpn() != null) {
                wmsShelvesTaskPdaDetailed.setLpn(wmsShelves.getLpn());
            } else {
                wmsShelvesTaskPdaDetailed.setLpn("0");
            }

            //货品id
            wmsShelvesTaskPdaDetailed.setHpid(wmsShelves.getHpid());
            //收货库位名称
            wmsShelvesTaskPdaDetailed.setShkwmc(wmsShelves.getShkwmc());
            //添加PDA上架明细
            i = dao.insertShelvesPdaDetailed(wmsShelvesTaskPdaDetailed);
            if (i > 0) {
                i = dao.updateWms_receiving(wmsShelves.getSsshid(), "", "1", "1");
                if (i > 0) {
                } else {
                    throw new RuntimeException("收货表生成状态更改失败");
                }
            } else {
                throw new RuntimeException("PDA上架任务明细生成失败");
            }
        }
    }
    /**
     * PDA消息推送
     *
     * @param wmsPdaMsg
     * @param wmsEstimatedArrivalOrder
     * @param wmsShelves1
     * @throws Exception
     */
    @Transactional
    public void saveMsg(WmsPdaMsg wmsPdaMsg, WmsEstimatedArrivalOrder wmsEstimatedArrivalOrder, WmsShelves wmsShelves1) throws Exception {
        //pda信息推送表添加数据
        wmsPdaMsg = new WmsPdaMsg();
        //标题
        wmsPdaMsg.setBt(wmsEstimatedArrivalOrder.getAsnbh());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date time = new Date();
        String bcTime = sdf.format(time);
        //消息
        wmsPdaMsg.setXx(bcTime + "生成上架任务");
        //状态
        wmsPdaMsg.setZt("0");
        //人员id
        wmsPdaMsg.setRyid(String.valueOf(wmsShelves1.getCzrId()));
        //PDA消息表添加
        Integer i = dao.saveMsg(wmsPdaMsg);
        if (i <= 0) {
            throw new RuntimeException("PDA拣货任务消息推送失败");
        }
    }
    /**
     * PDA上架信息回滚
     *
     * @param wmsShelvesTaskPda2
     * @param wmsShelvesTaskPdaDetailedList
     * @param wmsReceivingsList
     */
    public void rollBack2(WmsShelvesTaskPda wmsShelvesTaskPda2, List<Integer> wmsShelvesTaskPdaDetailedList, List<WmsReceiving> wmsReceivingsList) {
        if (wmsShelvesTaskPda2 != null) {
            //删除PDA上架信息
            dao.deleteWmsShelvesTaskPda(wmsShelvesTaskPda2.getId());
        }
        if (wmsShelvesTaskPdaDetailedList != null && wmsShelvesTaskPdaDetailedList.size() > 0) {
            //删除PDA上架明细信息
            dao.deletePdaDetailed(wmsShelvesTaskPdaDetailedList);
        }
        if (wmsReceivingsList != null) {
            //修改收货表状态
            for (WmsReceiving wmsReceiving : wmsReceivingsList) {
                wmsReceiving.setSczt("0");
                dao.updateWms_receiving(wmsReceiving.getId(), "", wmsReceiving.getSczt(), wmsReceiving.getCzfs());
            }
        }

    }
    /*@BaseServiceDescribe(moduleName = "上架管理", btnName = "上架任务确认保存")
    @Transactional
    public void addRW(List<WmsShelves> list,String ckid,String hzid) throws RuntimeException{
        //存储上架生成库存信息(用于回滚)
        List<WmsStock> wmsStockList1=new ArrayList<WmsStock>();
        //存储更改收货库存信息(用于回滚)
        List<WmsStock> wmsStockList2=new ArrayList<WmsStock>();
        //存储上架保存信息(用于回滚)
        List<WmsShelves> wmsShelvesList=new ArrayList<WmsShelves>();
        //存储收货表更改信息(用于回滚)
        List<WmsReceiving> wmsReceivingsList=new ArrayList<WmsReceiving>();
        try {
            String yqdhbdid=list.get(0).getYqdhbdid();
            //dao.deleteSj(yqdhbdid);
            for(WmsShelves wmsShelves:list){
                WmsLibraryPosition wmsLibraryPosition=dao.searchKwHphf(wmsShelves.getKwid());
                //是否可以货品混放 0不可 1允许(hphf)
                //同货品批次是否可以混放 0不可 1允许
                //查询库位对应的库存内的货品
                List<WmsStock> wmsStockList = dao.searchHpKc(wmsShelves.getKwid());
                Integer k=wmsStockList.size();
                if(k>0){
                    for(WmsStock wmsStock:wmsStockList){
                        //判断货品相同
                        if(wmsShelves.getShow_bh().equals(wmsStock.getHpbh())){
                            //判断同货品批次是否相同
                            String hppc1=wmsShelves.getShow_hppc();
                            String hppc2=wmsStock.getHppc();
                            if(hppc1.equals(hppc2)){
                                //货品以及批次都相同
                                //运行代码
                                addFZ(wmsShelves,ckid,hzid,wmsStockList1,wmsStockList2,wmsShelvesList,wmsReceivingsList);
                            }else{
                                //批次不相同
                                //同货品批次是否可以混放 0不可 1允许(pchf)
                                if("0".equals(wmsLibraryPosition.getPchf())){
                                    rollBack(wmsStockList1,wmsStockList2,wmsShelvesList,wmsReceivingsList);
                                    throw new RuntimeException(wmsShelves.getShow_kwid()+"库位同货品不同批次不可以混放");
                                }else{
                                    //货品相同,批次不相同
                                    //运行代码
                                    addFZ(wmsShelves,ckid,hzid,wmsStockList1,wmsStockList2,wmsShelvesList,wmsReceivingsList);
                                }
                            }
                        }else {
                            //货品不相同
                            //是否可以货品混放 0不可 1允许(hphf)
                            if("0".equals(wmsLibraryPosition.getHphf())){
                                rollBack(wmsStockList1,wmsStockList2,wmsShelvesList,wmsReceivingsList);
                                throw new RuntimeException(wmsShelves.getShow_kwid()+"库位不同货品不可以混放");
                            }else{
                                //判断同货品批次是否相同
                                if(wmsShelves.getShow_hppc().equals(wmsStock.getHppc())){
                                    //货品以及批次都不相同
                                    //运行代码
                                    addFZ(wmsShelves,ckid,hzid,wmsStockList1,wmsStockList2,wmsShelvesList,wmsReceivingsList);
                                }else{
                                    //批次不相同
                                    //同货品批次是否可以混放 0不可 1允许(pchf)
                                    if("0".equals(wmsLibraryPosition.getPchf())){
                                        rollBack(wmsStockList1,wmsStockList2,wmsShelvesList,wmsReceivingsList);
                                        throw new RuntimeException(wmsShelves.getShow_kwid()+"库位同货品不同批次不可以混放");
                                    }else{
                                        //货品不相同,批次不相同
                                        //运行代码
                                        addFZ(wmsShelves,ckid,hzid,wmsStockList1,wmsStockList2,wmsShelvesList,wmsReceivingsList);
                                    }
                                }
                            }
                        }
                    }

                }else{
                    //库位内没有库存数据
                    addFZ(wmsShelves,ckid,hzid,wmsStockList1,wmsStockList2,wmsShelvesList,wmsReceivingsList);
                }
            }
        } catch (Exception e) {
            rollBack(wmsStockList1,wmsStockList2,wmsShelvesList,wmsReceivingsList);
            throw new RuntimeException("上架任务确认保存失败", e);
        }
    }*/

    /**
     * 生成任务查询操作人
     * @param czr
     * @param ckid
     * @param hzid
     * @return
     */
    public List<WmsShelves> searchRwCzr(String czr, String ckid, String hzid) {
        return dao.searchRwCzr(czr, ckid, hzid);
    }
    /**
     *查看任务详情页面查询
     * @param entity
     * @return
     */
    public Page<WmsShelves> selectRw(WmsShelves entity) throws Exception {
        try {
            //货主订单类型表:是否质检(0否 1是)
            String sfzj1 = "1";
            String zjzt;
            //查询当前货主信息
            WmsOwner wmsOwner = dao.searchZjfs(entity.getHzid());
            String sfzj2 = dao.searchDdlx(entity.getYqdhbdid(), entity.getHzid());
            if (sfzj1.equals(wmsOwner.getZjfs())) {
                if (sfzj1.equals(sfzj2)) {
                    zjzt = "2";
                    PageHelper.startPage(entity.getPage(), entity.getRows());
                    return (Page<WmsShelves>) dao.selectRw(entity.getYqdhbdid(), entity.getHzid(), entity.getCkid(), zjzt, null);
                } else {
                    zjzt = "3";
                    PageHelper.startPage(entity.getPage(), entity.getRows());
                    return (Page<WmsShelves>) dao.selectRw(entity.getYqdhbdid(), entity.getHzid(), entity.getCkid(), null, zjzt);
                }
            } else {
                zjzt = "3";
                PageHelper.startPage(entity.getPage(), entity.getRows());
                return (Page<WmsShelves>) dao.selectRw(entity.getYqdhbdid(), entity.getHzid(), entity.getCkid(), null, zjzt);
            }
        } catch (Exception e) {
            throw new ServiceException("查询任务详情信息失败", e);
        }
    }
}
